home *** CD-ROM | disk | FTP | other *** search
/ AmigActive 22 / AACD 22.iso / AACD / Online / Apache / lib / php / DB / odbc.php < prev    next >
Encoding:
PHP Script  |  2001-03-06  |  6.3 KB  |  247 lines

  1. <?php
  2. //
  3. // +----------------------------------------------------------------------+
  4. // | PHP version 4.0                                                      |
  5. // +----------------------------------------------------------------------+
  6. // | Copyright (c) 1997, 1998, 1999, 2000 The PHP Group                   |
  7. // +----------------------------------------------------------------------+
  8. // | This source file is subject to version 2.02 of the PHP license,      |
  9. // | that is bundled with this package in the file LICENSE, and is        |
  10. // | available at through the world-wide-web at                           |
  11. // | http://www.php.net/license/2_02.txt.                                 |
  12. // | If you did not receive a copy of the PHP license and are unable to   |
  13. // | obtain it through the world-wide-web, please send a note to          |
  14. // | license@php.net so we can mail you a copy immediately.               |
  15. // +----------------------------------------------------------------------+
  16. // | Authors: Stig Bakken <ssb@fast.no>                                   |
  17. // |                                                                      |
  18. // +----------------------------------------------------------------------+
  19. //
  20. // Database independent query interface definition for PHP's ODBC
  21. // extension.
  22. //
  23.  
  24. //
  25. // XXX legend:
  26. //
  27. // XXX ERRORMSG: The error message from the odbc function should
  28. //                 be registered here.
  29. // XXX ADDREF:     As soon as Zend/PHP gets support for returning
  30. //                 references, this return value should be made into
  31. //                 a reference.
  32. //
  33.  
  34. require_once 'DB/common.php';
  35.  
  36. class DB_odbc extends DB_common {
  37.     // {{{ properties
  38.  
  39.     var $connection;
  40.     var $phptype, $dbsyntax;
  41.  
  42.     // }}}
  43.  
  44.     // {{{ constructor
  45.  
  46.     function DB_odbc() {
  47.         $this->phptype = 'odbc';
  48.         $this->dbsyntax = 'unknown';
  49.         // Let's be very pessimistic about the features of an odbc
  50.         // backend.  The user has to specify the dbsyntax to access
  51.         // other features.
  52.         $this->features = array(
  53.             'prepare' => false,
  54.             'pconnect' => true,
  55.             'transactions' => false
  56.         );
  57.         $this->errorcode_map = array(
  58.             "01004" => DB_ERROR_TRUNCATED,
  59.             "07001" => DB_ERROR_MISMATCH,
  60.             "21S01" => DB_ERROR_MISMATCH,
  61.             "21S02" => DB_ERROR_MISMATCH,
  62.             "22003" => DB_ERROR_INVALID_NUMBER,
  63.             "22008" => DB_ERROR_INVALID_DATE,
  64.             "22012" => DB_ERROR_DIVZERO,
  65.             "23000" => DB_ERROR_CONSTRAINT,
  66.             "24000" => DB_ERROR_INVALID,
  67.             "34000" => DB_ERROR_INVALID,
  68.             "37000" => DB_ERROR_SYNTAX,
  69.             "42000" => DB_ERROR_SYNTAX,
  70.             "IM001" => DB_ERROR_UNSUPPORTED,
  71.             "S0001" => DB_ERROR_NOT_FOUND,
  72.             "S0002" => DB_ERROR_NOT_FOUND,
  73.             "S0011" => DB_ERROR_ALREADY_EXISTS,
  74.             "S0012" => DB_ERROR_NOT_FOUND,
  75.             "S0021" => DB_ERROR_ALREADY_EXISTS,
  76.             "S0022" => DB_ERROR_NOT_FOUND,
  77.             "S1009" => DB_ERROR_INVALID,
  78.             "S1090" => DB_ERROR_INVALID,
  79.             "S1C00" => DB_ERROR_NOT_CAPABLE
  80.         );
  81.     }
  82.  
  83.     // }}}
  84.  
  85.     // {{{ connect()
  86.  
  87.     /**
  88.      * Connect to a database and log in as the specified user.
  89.      *
  90.      * @param $dsn the data source name (see DB::parseDSN for syntax)
  91.      * @param $persistent (optional) whether the connection should
  92.      *        be persistent
  93.      *
  94.      * @return int DB_OK on success, a DB error code on failure
  95.      */
  96.     function connect(&$dsn, $user, $pw) {
  97.         if (is_array($dsn)) {
  98.             $dsninfo = &$dsn;
  99.         } else {
  100.             $dsninfo = DB::parseDSN($dsn);
  101.         }
  102.         if (!$dsninfo || !$dsninfo['phptype']) {
  103.             return $this->raiseError(); // XXX ERRORMSG
  104.         }
  105.         $this->dbsyntax = $dsninfo['dbsyntax'];
  106.         switch ($this->dbsyntax) {
  107.             case 'solid':
  108.                 $this->features = array(
  109.                     'prepare' => true,
  110.                     'pconnect' => true,
  111.                     'transactions' => true
  112.                 );
  113.                 $default_dsn = 'localhost';
  114.                 break;
  115.             default:
  116.                 break;
  117.         }
  118.         $dbhost = $dsninfo['hostspec'] ? $dsninfo['hostspec'] : 'localhost';
  119.         $user = $dsninfo['username'];
  120.         $pw = $dsninfo['password'];
  121.         if ($this->provides('pconnect')) {
  122.             $connect_function = $persistent ? 'odbc_pconnect' : 'odbc_connect';
  123.         } else {
  124.             $connect_function = 'odbc_connect';
  125.         }
  126.         if ($dbhost && $user && $pw) {
  127.             $conn = $connect_function($dbhost, $user, $pw);
  128.         } elseif ($dbhost && $user) {
  129.             $conn = $connect_function($dbhost, $user);
  130.         } elseif ($dbhost) {
  131.             $conn = $connect_function($dbhost);
  132.         } else {
  133.             $conn = false;
  134.         }
  135.         if ($conn == false) {
  136.             return $this->raiseError(); // XXX ERRORMSG
  137.         }
  138.         $this->connection = $conn;
  139.         return DB_OK;
  140.  
  141.  
  142.  
  143.         $this->connection = odbc_connect($dsn, $user, $pw);
  144.     }
  145.  
  146.     // }}}
  147.     // {{{ disconnect()
  148.  
  149.     function disconnect() {
  150.         $err = odbc_close($this->connection); // XXX ERRORMSG
  151.         return $err;
  152.     }
  153.  
  154.     // }}}
  155.     // {{{ query()
  156.  
  157.     function query($query) {
  158.         $this->last_query = $query;
  159.         $result = odbc_exec($this->connection, $query);
  160.         if (!$result) {
  161.             return $this->raiseError(); // XXX ERRORMSG
  162.         }
  163.         // Determine which queries that should return data, and which
  164.         // should return an error code only.
  165.         if (preg_match('/SELECT/i', $query)) {
  166.             $resultObj = new DB_result($this, $result);
  167.             return $resultObj; // XXX ADDREF
  168.         } else {
  169.             return DB_OK;
  170.         }
  171.     }
  172.  
  173.     // }}}
  174.     // {{{ simpleQuery()
  175.  
  176.     /**
  177.      * Send a query to ODBC and return the results as a ODBC resource
  178.      * identifier.
  179.      *
  180.      * @param $query the SQL query
  181.      *
  182.      * @return int returns a valid ODBC result for successful SELECT
  183.      * queries, DB_OK for other successful queries.  A DB error code
  184.      * is returned on failure.
  185.      */
  186.     function simpleQuery($query) {
  187.         $this->last_query = $query;
  188.         $result = odbc_exec($this->connection, $query);
  189.         if (!$result) {
  190.             return $this->raiseError(); // XXX ERRORMSG
  191.         }
  192.         // Determine which queries that should return data, and which
  193.         // should return an error code only.
  194.         if (preg_match('/SELECT/i', $query)) {
  195.             return $result;
  196.         } else {
  197.             return DB_OK;
  198.         }
  199.     }
  200.  
  201.     // }}}
  202.     // {{{ fetchRow()
  203.  
  204.     function fetchRow($result) {
  205.         $cols = odbc_fetch_into($result, &$row);
  206.         if ($cols == 0) {
  207.             // XXX ERRORMSG
  208.             return false;
  209.         }
  210.         return $row; // XXX ADDREF
  211.     }
  212.  
  213.     // }}}
  214.     // {{{ freeResult()
  215.  
  216.     function freeResult($result) {
  217.         $err = odbc_free_result($result); // XXX ERRORMSG
  218.         return $err;
  219.     }
  220.  
  221.     // }}}
  222.     // {{{ quoteString()
  223.  
  224.     function quoteString($string) {
  225.         return str_replace("'", "''", $string);
  226.     }
  227.  
  228.     // }}}
  229.  
  230.     // prepare
  231.     // execute
  232.     // errorCode
  233.     // errorMsg
  234.     // errorNative
  235.     // longReadlen
  236.     // binMode
  237.     // autoCommit
  238.     // commit
  239.     // rollback
  240. }
  241.  
  242. // Local variables:
  243. // tab-width: 4
  244. // c-basic-offset: 4
  245. // End:
  246. ?>
  247.